home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / 07.09 Sep 91 / FilePaths Code ƒ / FilePaths.c next >
Encoding:
Text File  |  1990-07-05  |  5.1 KB  |  138 lines  |  [TEXT/KAHL]

  1. char *GetFilePathName(vRefNum)
  2.     short    vRefNum;                /* File's vol/dir reference        */
  3.     {
  4.         WDParam            wDir;        /* Working directory            */
  5.         HVolumeParam    wVol;        /* Working HFS param block        */
  6.         DirInfo            wCInfo;        /* Working catalog info block    */
  7.         long            wDirID;        /* Working working dir number    */
  8.         Str255            wName;        /* Working directory name        */
  9.         char            *wPtr;        /* Working string pointer        */
  10.         long            wLength;    /* Working string length        */
  11.         char            *pathFileName;/* Working file path name        */
  12.  
  13.         wDir.ioNamePtr = 0L;        /* Init working directory        */
  14.         wDir.ioVRefNum = vRefNum;
  15.         wDir.ioWDIndex = 0;
  16.         wDir.ioWDProcID = 0;
  17.         wDir.ioWDVRefNum = vRefNum;
  18.  
  19.         PBGetWDInfo(&wDir,FALSE);    /* Get the directory reference    */
  20.         
  21.         vRefNum = wDir.ioWDVRefNum;    /* Save working vol ref number    */
  22.         wDirID = wDir.ioWDDirID;    /* Save working dir ref number    */
  23.  
  24.         wVol.ioNamePtr = (StringPtr)&wName;/* Init volume data block*/
  25.         wVol.ioVRefNum = vRefNum;
  26.         wVol.ioVolIndex = 0;
  27.                 
  28.         wLength = 0L;                /* Set file's path length to zip*/
  29.         pathFileName = NewPtr(0L);    /* Set null file's path            */
  30.         
  31.         if (!PBHGetVInfo(&wVol,FALSE) &&/* Check if got vol info    */
  32.             pathFileName) {            /* Check if got file path pointer*/
  33.             if (wVol.ioVSigWord == 0x4244) {/* Check if it's HFS    */
  34.                 wCInfo.ioNamePtr = (StringPtr)&wName;/* Init cat info*/
  35.                 wCInfo.ioVRefNum = vRefNum;
  36.                 wCInfo.ioFDirIndex = -1;
  37.                 wCInfo.ioDrParID = wDirID;
  38.                 wCInfo.ioDrDirID = wDirID;
  39.                 while (wCInfo.ioDrParID != 1){/* Do full path        */            
  40.                     wCInfo.ioDrDirID = wCInfo.ioDrParID;/* Move up 1 dir*/
  41.                     if (PBGetCatInfo(&wCInfo,FALSE))/* Get dir info    */
  42.                         break;        /* Break-out if failed!!        */
  43.                     wLength += wName[0] + 1L;/* Set string length    */
  44.                     wPtr = NewPtr(wLength + 1L);/* Alloc new str    */
  45.                     if (!wPtr) {    /* Check if didn't get str ptr    */
  46.                         if (pathFileName)/* Check if got any memory    */
  47.                             DisposPtr(pathFileName);/* Release it    */
  48.                         pathFileName = 0L;/* Invalidate str pointer    */
  49.                         break;        /* Break-out if failed!!        */
  50.                     }
  51.                                     /* Shuffle the file path down    */
  52.                     BlockMove(pathFileName,wPtr + wName[0] + 1,
  53.                                 wLength - (long)(wName[0]));
  54.                     DisposPtr(pathFileName);/* Release old one        */
  55.                     *(wPtr + wName[0]) = ':';/* Add dir delimeter    */
  56.                     BlockMove(&wName[1],pathFileName = wPtr,
  57.                               (long)(wName[0]));
  58.                 }
  59.             }
  60.             else {                    /* Oops, file to get vol info    */
  61.                 wLength = wName[0] + 1L;/* Set string length        */
  62.                 wPtr = NewPtr(wLength + 1L);/* Alloc new string        */
  63.                 if (wPtr) {            /* Check if got the string pointer*/
  64.                     *(wPtr + wName[0]) = ':';/* Tack on dir delimeter*/
  65.                     BlockMove(&wName[1],pathFileName = wPtr,
  66.                               (long)(wName[0]));
  67.                 }
  68.             }
  69.             if (pathFileName)        /* Check if got da string        */
  70.                 pathFileName[wLength] = 0;/* Set end-of-string    */
  71.         }
  72.         return(pathFileName);        /* Return file path name        */
  73.     }
  74.     
  75. short GetFilePathVolRef(pathFileName)
  76.     char    *pathFileName;            /* File's path string            */
  77.     {
  78.     
  79.         short            i;            /* Working index                */
  80.         char            c;            /* Working input character        */
  81.         short            vRefNum;    /* Working vol/dir reference    */
  82.         WDParam            wDir;        /* Working directory            */
  83.         HVolumeParam    wVol;        /* Working HFS param block        */
  84.         DirInfo            wCInfo;        /* Working catalog info block    */
  85.         long            wDirID;        /* Working directory ID            */
  86.         Str255            wVolName;    /* Working volume name            */
  87.         Str255            wName;        /* Working string name            */
  88.         char            *wPtr;        /* Working string pointer        */
  89.         short            wLength;    /* Working string length        */
  90.         
  91.         vRefNum = 0;                /* Invalidate vol/dir ref nbr    */
  92.         
  93.         wVol.ioVRefNum = 0;            /* Init working volume ID number*/
  94.         wCInfo.ioDrDirID = 2;        /* Init top-most directory        */
  95.         
  96.         i = 0;                        /* Init working index            */
  97.         wLength = 0;                /* Init string length            */
  98.         
  99.         c = pathFileName[i];        /* Set first input character    */
  100.         
  101.         while(c) {                    /* Do until got vol/dir ref nbr    */
  102.             if (c == ':') {            /* Check if got dir specs        */
  103.                 wName[0] = wLength - 1;/* Set "Pascal" string length*/
  104.                 wLength = 0;        /* Reset string length            */
  105.                 if (!wVol.ioVRefNum) {/* Check if need volume reference*/
  106.                     wVol.ioNamePtr = (StringPtr)(&wVolName);/* Init blk*/
  107.                     for(wVol.ioVolIndex = 1; !PBHGetVInfo(&wVol,FALSE);
  108.                         wVol.ioVolIndex++) {
  109.                         if (EqualString(wName,wVolName,FALSE,FALSE))
  110.                             break;
  111.                     }
  112.                     vRefNum = wVol.ioVRefNum;/* Save vol ID            */
  113.                     if (wVol.ioVSigWord != 0x4244)/* MFS ?            */
  114.                         return(vRefNum);/* Let's get out early!        */
  115.                 }
  116.                 else {                /* Nope, get directory reference*/
  117.                     wCInfo.ioNamePtr = (StringPtr)(&wName);/* Init blk*/
  118.                     wCInfo.ioVRefNum = wVol.ioVRefNum;
  119.                     wCInfo.ioFDirIndex = 0;
  120.                     wCInfo.ioDrParID = wCInfo.ioDrDirID;
  121.                     if (PBGetCatInfo(&wCInfo,FALSE))/* Check dir ?    */
  122.                         return(0);    /* Nope, break-out!!!            */
  123.                 }
  124.             }
  125.             c = wName[++wLength] = pathFileName[i++];/* Add to dir spec*/
  126.         }
  127.         wDir.ioNamePtr = 0L;        /* Init directory data block    */
  128.         wDir.ioVRefNum = wVol.ioVRefNum;
  129.         wDir.ioWDProcID = 'ERIK';    /* Magic 'SFGetFile' proc ID #    */
  130.         wDir.ioWDDirID = wCInfo.ioDrDirID;
  131.         
  132.         if (!PBOpenWD(&wDir,FALSE))    /* Check if opened directory    */
  133.             vRefNum = wDir.ioVRefNum;/* Return vol/dir ref number    */
  134.  
  135.         return(vRefNum);            /* Return vol/dir ref number    */
  136.     }
  137.     
  138.